package com.ygqh.baby.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.WdtQybStockInDetailMapper;
import com.ygqh.baby.mapper.WdtQybStockInMapper;
import com.ygqh.baby.po.WdtQybStockIn;
import com.ygqh.baby.po.WdtQybStockInDetail;
import com.ygqh.baby.po.WdtQybStockInDetailExample;
import com.ygqh.baby.po.WdtQybStockInExample;
import com.ygqh.baby.service.WdtQybStockInService;

@Service
public class WdtQybStockInServiceImpl implements WdtQybStockInService {

	@Autowired
	private WdtQybStockInMapper wdtQybStockInMapper;
	@Autowired
	private WdtQybStockInDetailMapper wdtQybStockInDetailMapper;

	@Override
	public List<WdtQybStockIn> find() {
		WdtQybStockInExample example = new WdtQybStockInExample();
		example.createCriteria();
		return wdtQybStockInMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		WdtQybStockInExample example = new WdtQybStockInExample();
		example.createCriteria();
		return wdtQybStockInMapper.countByExample(example);
	}

	@Override
	public WdtQybStockIn findById(Integer id) {
		WdtQybStockInDetailExample example = new WdtQybStockInDetailExample();
		example.createCriteria().andStockinIdEqualTo(id);
		List<WdtQybStockInDetail> list = wdtQybStockInDetailMapper.selectByExample(example);
		WdtQybStockIn stockIn = wdtQybStockInMapper.selectByPrimaryKey(id);
		stockIn.setDetailsList(list);
		return stockIn;
	}

	@Override
	public int save(WdtQybStockIn wdtQybStockIn) {
		return wdtQybStockInMapper.insertSelective(wdtQybStockIn);
	}

	@Override
	public int update(WdtQybStockIn wdtQybStockIn) {
		return wdtQybStockInMapper.updateByPrimaryKeySelective(wdtQybStockIn);
	}

	@Override
	public ResultSet<WdtQybStockIn> search(QueryInfo queryInfo, String q) {
		List<WdtQybStockIn> list = wdtQybStockInMapper.selectSuper(queryInfo, q);
		int count = wdtQybStockInMapper.countSuper(q);
		ResultSet<WdtQybStockIn> resultSet = new ResultSet<WdtQybStockIn>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(WdtQybStockIn wdtQybStockIn) {
		int r = 0;
		if (wdtQybStockIn.getStockinId() != null) {
			r = this.update(wdtQybStockIn);
		} else {
			r = this.save(wdtQybStockIn);
		}
		return r;
	}

	@Override
	public int addStockIn(WdtQybStockIn in) {
		int i = this.save(in);
		if (i > 0) {
			wdtQybStockInDetailMapper.insertBatch(in.getDetailsList());
		}
		return i;
	}

	@Override
	public WdtQybStockIn findByStockInId(Integer stockinId) {
		WdtQybStockInExample example = new WdtQybStockInExample();
		example.createCriteria().andStockinIdEqualTo(stockinId);
		List<WdtQybStockIn> list = wdtQybStockInMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(list)) {

			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WdtQybStockInDetail> findStockInDetailByStockInId(Integer stockinId) {
		WdtQybStockInDetailExample detailExample = new WdtQybStockInDetailExample();
		detailExample.createCriteria().andStockinIdEqualTo(stockinId);

		return wdtQybStockInDetailMapper.selectByExample(detailExample);
	}

}
